package com.sc.nft.dao;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MetaMapSettle;
import com.sc.nft.entity.vo.metamap.MetaMapDividendsVO;
import com.sc.nft.enums.MetaMapSettleSourceTypeEnum;
import com.sc.nft.enums.MetaMapSettleStatusEnum;
import com.sc.nft.mapper.MetaMapSettleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author PC
 * @description 针对表【meta_map_settle】的数据库操作Dao
 * @createDate 2024-09-04 18:21:59
 */
@Slf4j
@Repository
public class MetaMapSettleDao extends ServiceImpl<MetaMapSettleMapper, MetaMapSettle> {

    /**
     * 获取生态熔炼待发放数据
     * @return MetaMapSettle
     */
    public List<MetaMapSettle> listSmelting() {
        LambdaQueryWrapper<MetaMapSettle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MetaMapSettle::getStatus, MetaMapSettleStatusEnum.TO_BE_ISSUED);
        wrapper.eq(MetaMapSettle::getSourceType, MetaMapSettleSourceTypeEnum.SMELTING);
        wrapper.lt(MetaMapSettle::getGetTime, LocalDateTime.now().with(LocalTime.MIDNIGHT));
        return list(wrapper);
    }

    /**
     *
     * @param pageNo pageNo
     * @param pageSize pageSize
     * @param dividendsTypeEnum dividendsTypeEnum
     * @param name name
     * @param userTel userTel
     * @param startTime startTime
     * @param endTime endTime
     * @return MetaMapDividendsVO
     */
    public Page<MetaMapDividendsVO> pageFundDividends(Integer pageNo, Integer pageSize, MetaMapSettleSourceTypeEnum dividendsTypeEnum, String name, String userTel, Date startTime, Date endTime, MetaMapSettleStatusEnum status) {
        return baseMapper.pageFundDividends(new Page<>(pageNo, pageSize), Objects.nonNull(dividendsTypeEnum) ? dividendsTypeEnum.getCode() : null, name, userTel, startTime, endTime, Objects.nonNull(status) ? status.getCode() : null);
    }

    public Boolean updateSettleSendStatus(Long id, Integer version, MetaMapSettleStatusEnum metaMapSettleStatusEnum, Date date) {
        LambdaUpdateWrapper<MetaMapSettle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MetaMapSettle::getId, id);
        wrapper.set(MetaMapSettle::getVersion, version);
        wrapper.set(MetaMapSettle::getPaymentTime, date);
        wrapper.set(MetaMapSettle::getStatus, metaMapSettleStatusEnum);
        return update(wrapper);
    }


    /**
     * 获取分润数据
     * @return
     */
    public List<MetaMapSettle> getShareProfitList() {
        LambdaUpdateWrapper<MetaMapSettle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MetaMapSettle::getStatus, MetaMapSettleStatusEnum.TO_BE_ISSUED);
        wrapper.in(MetaMapSettle::getSourceType, MetaMapSettleSourceTypeEnum.FIRST_ORDER, MetaMapSettleSourceTypeEnum.SECOND_ORDER, MetaMapSettleSourceTypeEnum.PROP_ORDER, MetaMapSettleSourceTypeEnum.SHOP);
        wrapper.last("limit 1000");
        return list(wrapper);
    }


    public Boolean settleAnAccountSuccess(Long id) {
        LambdaUpdateWrapper<MetaMapSettle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MetaMapSettle::getId, id);
        wrapper.set(MetaMapSettle::getPaymentTime, DateUtil.date());
        wrapper.set(MetaMapSettle::getStatus, MetaMapSettleStatusEnum.ISSUED);
        return update(wrapper);
    }

    /**
     * 获取商城订单
     * @param orderNo
     * @return
     */
    public MetaMapSettle getMallOrderNo(String orderNo) {
        LambdaQueryWrapper<MetaMapSettle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MetaMapSettle::getOrderNo, orderNo);
        wrapper.eq(MetaMapSettle::getSourceType, MetaMapSettleSourceTypeEnum.MALL);
        return getOne(wrapper);
    }
}
